package com.tpc.client.systeme.core.datasource;

import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.core.client.GWT;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.tpc.client.systeme.core.service.UserService;
import com.tpc.client.systeme.core.service.UserServiceAsync;
import com.tpc.dao.extend.Ressource;

public class RessourceDataSource extends GwtRpcDataSource {

    String strurl = null;

    public RessourceDataSource(String strID) {
            setID(strID);

            DataSourceIntegerField ressourceId = new DataSourceIntegerField("ressourceId");
            ressourceId.setPrimaryKey(Boolean.TRUE);
            DataSourceTextField ressourceName = new DataSourceTextField("ressourceName", " ");

            setFields(ressourceName,ressourceId);

            strurl = Page.getAppDir();
            strurl = strurl.substring(0,strurl.length()-1);
            strurl = strurl.substring(strurl.lastIndexOf("/"));
            
    }

    @Override
    protected void executeFetch (final String requestId, final DSRequest request, final DSResponse response) {
        UserServiceAsync service = GWT.create(UserService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        endpoint.setServiceEntryPoint(strurl+"/com.tpc.user/usrloader");
        service.loadListRessource(new AsyncCallback<Ressource[]> () {
            public void onFailure (Throwable caught) {
                response.setStatus (RPCResponse.STATUS_FAILURE);
                processResponse (requestId, response);
            }
            public void onSuccess (Ressource[] result) {
                ListGridRecord[] list = null;
                if (result != null) {
                    list = new ListGridRecord[result.length];
                    for (int i = 0; i < result.length; i++) {
                        list[i] = new ListGridRecord();
                        list[i].setAttribute("ressourceName", result[i].getFirstName()+" "+result[i].getLastName());
                        list[i].setAttribute("ressourceId", result[i].getId());
                    }
                }
                response.setData(list);
                processResponse(requestId, response);
            }
        });

    }

    @Override
    protected void executeAdd (final String requestId, final DSRequest request, final DSResponse response) {
        //@TODO : fdsa
    }

    @Override
    protected void executeUpdate (final String requestId, final DSRequest request, final DSResponse response) {
         //@TODO : fdsa
    }

    @Override
    protected void executeRemove (final String requestId, final DSRequest request, final DSResponse response) {
         //@TODO : fdsa
    }

}
